package com.yxgc.chat.wildfire.nativelib;

import com.facebook.react.bridge.Callback;
import com.google.gson.Gson;
import com.yxgc.chat.wildfire.nativelib.bean.UIUserInfo;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cn.wildfirechat.message.MessageContent;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.FriendRequest;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.ModifyGroupInfoType;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;
import cn.wildfirechat.remote.GeneralCallback;
import cn.wildfirechat.remote.GeneralCallback2;

class ContractModel {
    private Gson gson;

    public ContractModel() {
        gson = new Gson();
    }

    /**
     * 获取通讯录
     */
    public void getFriendList(Callback successCallback) {
        List<UserInfo> userInfos = ChatManager.Instance().getMyFriendListInfo(false);
        successCallback.invoke(gson.toJson(UIUserInfo.fromUserInfos(userInfos)));
    }

    /**
     * 添加好友
     */
    public void addFriend(String targetUid, String reason, Callback callback) {
        ChatManager.Instance().sendFriendRequest(targetUid, reason, null, new GeneralCallback() {
            @Override
            public void onSuccess() {
                callback.invoke(true);
            }

            @Override
            public void onFail(int errorCode) {
                callback.invoke(false);
            }
        });
    }
    /**
     * incoming true，只包含收到的好友请求；false，所有好友请求
     * */
    public void getFriendRequest(Callback callback){
        List<FriendRequest>  friendRequests =  ChatManager.Instance().getFriendRequest(true);
        callback.invoke(gson.toJson(friendRequests));
    }
    public void handleFriendRequest(String friendId,Callback callback) {
        ChatManager.Instance().handleFriendRequest(friendId, true, null, new GeneralCallback() {
            @Override
            public void onSuccess() {
                ChatManager.Instance().loadFriendRequestFromRemote();
                List<FriendRequest> inComingFriendRequests = ChatManager.Instance().getFriendRequest(true);
                for (FriendRequest request : inComingFriendRequests) {
                    if (request.target.equals(friendId)) {
                        callback.invoke(true);
                        return;
                    }
                }
                callback.invoke(false);
            }

            @Override
            public void onFail(int errorCode) {
                callback.invoke(false);
            }
        });
    }

    /**
     * 创建群聊
     */
    public void createGroup(String groupName, List<String> selectedIds,Callback callback) {
        ChatManager.Instance().createGroup(null, groupName, null, GroupInfo.GroupType.Normal, null, selectedIds, null, Arrays.asList(0), null, new GeneralCallback2() {
            @Override
            public void onSuccess(String groupId) {
                    callback.invoke(true);
            }

            @Override
            public void onFail(int errorCode) {
                callback.invoke(false);
            }
        });
    }
    /**
     * 修改群名称
     */
    public void modifyGroupName(String groupId, int type, String newValue, MessageContent notifyMsg, List<Integer> notifyLines) {
        ModifyGroupInfoType modifyType =  ModifyGroupInfoType.type(type);
        ChatManager.Instance().modifyGroupInfo(groupId, modifyType, newValue, notifyLines, notifyMsg, new GeneralCallback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    /**
     * 退出群聊
     */
    public void quiteGroup(String groupId,Callback callback) {
        ChatManager.Instance().quitGroup(groupId, Collections.singletonList(0), null, new GeneralCallback() {
            @Override
            public void onSuccess() {
                callback.invoke(true);
            }

            @Override
            public void onFail(int errorCode) {
                callback.invoke(false);
            }
        });
    }

    /**
     * 删除好友
     */
    public void deleteFriend(String userId,Callback callback) {
        ChatManager.Instance().deleteFriend(userId, new GeneralCallback() {
            @Override
            public void onSuccess() {
                ChatManager.Instance().removeConversation(new Conversation(Conversation.ConversationType.Single, userId, 0), true);
                callback.invoke(true);
            }

            @Override
            public void onFail(int errorCode) {
                callback.invoke(false);
            }
        });
    }

    /**
     * 设置用户昵称
     */
    public void setFriendAlias(String userId, String alias,Callback callback) {

        ChatManager.Instance().setFriendAlias(userId, alias, new GeneralCallback() {
            @Override
            public void onSuccess() {
                callback.invoke(true);
            }

            @Override
            public void onFail(int errorCode) {
                callback.invoke(false);
            }
        });

    }

    /**
     * 获取用户昵称
     */
    public void getFriendAlias(String userId,Callback callback) {
        callback.invoke(ChatManager.Instance().getFriendAlias(userId));
    }
}
